package com.ebelter.bodyfatscale.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import com.ebelter.bodyfatscale.R;
import com.ebelter.btlibrary.util.ULog;

/**
 * 仪表盘
 */

public class CeshiView extends View{

    // mPaint.setColor(getContext().getResources().getColor(R.color.bg_green));//这样获取颜色ok的
    //  mPaint.setColor(Color.parseColor("#232e40"));//这样获取颜色ok的

    private static final String TAG = "CeshiView";
    public CeshiView(Context context) {
        this(context,null);
    }
    public CeshiView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }


    int mWidth;
    int mHeight;

    Paint mPaint;

    int dashboard_blue_ff;
    int dashboard_blue_33;
    int dashboard_fa2b6e3;
    int transparent;


    private void init() {
        ULog.i(TAG,"---init() ---");
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);//抗锯齿

        mPaint.setColor(Color.BLUE);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(3);

        dashboard_blue_ff = getContext().getResources().getColor(R.color.dashboard_blue_ff);
        dashboard_blue_33 = getContext().getResources().getColor(R.color.dashboard_blue_33);
        dashboard_fa2b6e3 = getContext().getResources().getColor(R.color.dashboard_fa2b6e3);
        transparent = getContext().getResources().getColor(R.color.transparent);




    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        ULog.i(TAG,"---onSizeChanged() ---w="+w+"---h="+h+"---oldw"+oldw+"---oldh="+oldh);


    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        // ULog.i(TAG,"onMeasure()---屏幕的宽："+ DipUtil.getWindowWidth(getContext())+"---屏幕的高："+DipUtil.getWindowHeight(getContext()));
        int width = getMySize(100, widthMeasureSpec);
        int height = getMySize(100, heightMeasureSpec);
         ULog.i(TAG,"onMeasure()---控件的宽："+ width+"---控件的高："+height);
        setMeasuredDimension(width,height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        initBaseData();
        ULog.i(TAG,"----onDraw()---");
        mPaint.setStrokeWidth(10);

        canvas.drawText("(0,0)",0,0,mPaint);
        canvas.drawText("(1,0)",mWidth,0,mPaint);
        canvas.drawText("(1,1)",mWidth,mHeight,mPaint);
        canvas.drawText("(0,1)",0,mHeight,mPaint);


        canvas.drawLine(50,50,80,80,mPaint);
        canvas.drawCircle(mWidth/2,mHeight/2,10,mPaint);

        drawTrianglePointer(canvas);

    }

    Path path = new Path();
    float jianTouWidth=30;
    float jianTouHeight=60;
    float jianTouPanddingTop=30;
    private void drawTrianglePointer(Canvas canvas) {
        mPaint.setStrokeWidth(3);
        path.reset();
        path.moveTo(getWidth()/2, jianTouPanddingTop);//01
        path.lineTo(getWidth()/2+jianTouWidth/2, jianTouPanddingTop+jianTouHeight*0.425f);//02
        path.lineTo(getWidth()/2+jianTouWidth/5, jianTouPanddingTop+jianTouHeight*0.425f);//03
        path.lineTo(getWidth()/2+jianTouWidth/5, jianTouPanddingTop+jianTouHeight);//04
        path.lineTo(getWidth()/2-jianTouWidth/5, jianTouPanddingTop+jianTouHeight);//05
        path.lineTo(getWidth()/2-jianTouWidth/5, jianTouPanddingTop+jianTouHeight*0.425f);//06
        path.lineTo(getWidth()/2-jianTouWidth/2, jianTouPanddingTop+jianTouHeight*0.425f);//02
        path.lineTo(getWidth()/2, jianTouPanddingTop);//02
        path.close();
        //根据Path进行绘制，绘制三角形
        canvas.drawPath(path, mPaint);
    }

    private void initBaseData() {
        mWidth=getMeasuredWidth();
        mHeight=getMeasuredHeight();
    }

//---------------------------------------------------------------------------------------------------------------------
    private int getMySize(int defaultSize, int measureSpec) {
        int mySize = defaultSize;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);//这里的的尺寸大小并不是最终我们的View的尺寸大小，而是父View提供的参考大小
        switch (mode) {
            case MeasureSpec.UNSPECIFIED: {//如果没有指定大小，就设置为默认大小
                mySize = defaultSize;
                break;
            }
            case MeasureSpec.AT_MOST: {//如果测量模式是最大取值为size
                //我们将大小取最大值,你也可以取其他值
                mySize = size;
                break;
            }
            case MeasureSpec.EXACTLY: {//如果是固定的大小，那就不要去改变它
                mySize = size;
                break;
            }
        }
        return mySize;
    }
    public int dp2px(Context context, float dipValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }
}
